package top.wsuo.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import top.wsuo.factory.ServiceFactory;
import top.wsuo.model.Blog;

@WebServlet("/blog/modify.do")
public class BlogModifyAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public BlogModifyAction() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		request.setCharacterEncoding("UTF-8");

		int number = Integer.parseInt(request.getParameter("number"));
		String title = request.getParameter("title");
		String type = request.getParameter("type");
		String decoration = request.getParameter("decoration");
		String alter_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		String text = request.getParameter("text");
		Blog blog = new Blog();
		blog.setNumber(number);
		blog.setTitle(title);
		blog.setType(type);
		blog.setDecoration(decoration);
		blog.setAlter_time(alter_time);
		blog.setText(text);

		try {
			ServiceFactory.createBlogService().modifyBlog(blog);
			System.out.println("文章修改成功");
			response.sendRedirect("../home.jsp");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("文章修改失败");
			response.sendRedirect("modifyFailed.jsp");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		doGet(request, response);
	}

}
